// pages/login/login.js
import * as loginAPI from '../../api/loginApi';
import * as util from '../../utils/util';
let app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUse: true,
    sessionKey: null,
    openid: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
      this.loginAction()
      },
  loginAction(){
    wx.showLoading({})
      // 手机号码
      let that = this
      wx.login({

        success: function (res) {
    
          if (res.code) { //使用小程序登录的接口完成后端用户登录
            var code = res.code
            loginAPI.sendVCode (code).then (
              respon => {
                wx.hideLoading()
                wx.setStorage({
                  key:"openid",
                  data:respon.data.openid
                })
                app.globalData.userInfo.openid = respon.data.openid
                that.setData({
                  sessionKey: respon.data.sessionKey,
                  openid: respon.data.openid
                })
              },
              err => {
                wx.hideLoading()
                // util.toast (err || '数据错误,请稍后再试');
              }
            );
          } else {
            wx.hideLoading()
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      })
    },
    getPhoneNumber (e) {
      let encryptedData = e.detail.encryptedData
      let iv = e.detail.iv
      let that = this
      wx.showLoading({})
      if(e.detail.errMsg==='getPhoneNumber:ok'){
              let data = {
                cloudID: '',
                encryptedData: encryptedData,
                iv: iv,
                sessionKey: this.data.sessionKey,
                openid: this.data.openid,
              }
              loginAPI.login(data).then (
                respon => {
                  wx.setStorage({
                    key:"phone",
                    data:respon.data.phone,
                    success: function(res){
                      wx.setStorage({
                        key:"token",
                        data:respon.data.token,
                        success: function(res){
                          that.getUserInfo()
                        }
                      })
                    }
                  })
                  
                },
                err => {
                  wx.hideLoading()
                  // util.toast (err || '数据错误,请稍后再试');
                }
              );
            // }
          // })
      }else{
        wx.hideLoading()
        util.toast ('你已拒绝手机号授权');
      }
    },
    getUserInfo(){
      let token = wx.getStorageSync('token');
      loginAPI.getUserInfo().then(res => {
        wx.hideLoading()
        util.toast ('登录成功');
        app.globalData.userInfo.phone = res.data && res.data.user.user.phone
        app.globalData.userInfo.avatarPath = res.data && res.data.user.user.avatarPath
        app.globalData.userInfo.hasLogin = !!token
        app.globalData.userInfo.authentication = res.data && res.data.authentication
        // setTimeout(()=>{
          wx.switchTab({
            url: '/pages/me/me'
          })
        // }, 1200)
      })
    }
})